package com.swordget.service.impl;

import com.swordget.dao.TableDefinitionDao;
import com.swordget.entity.ColumnDesign;
import com.swordget.exception.VerifyFailException;
import com.swordget.service.TableDefinitionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: David
 * @Date: Created in 15:39 2020/4/8
 * @Description: 数据表机构管理服务实现
 * @Modified By:
 * @Version: 0.0.1
 */
@Service
public class TableDefinitionServiceImp implements TableDefinitionService {

    private TableDefinitionDao tableDefinitionDao;

    @Autowired
    public TableDefinitionServiceImp(TableDefinitionDao tableDefinitionDao) {
        this.tableDefinitionDao = tableDefinitionDao;
    }

    /**
     * @param tableId 表标识
     * @param withHidden 是否包含隐藏列
     * @param withBusiness
     * @return
     */
    @Override
    public List<String> getColumns(String tableId, boolean withHidden, boolean withBusiness) {
        //调用dao 层进行查询
        List<String> columns = tableDefinitionDao.getColumns(tableId, withHidden, withBusiness);
        return columns;
    }

    /**
     * @param tableId 表标识
     * @param withHidden 是否包含隐藏列
     * @param withBusiness
     * @return
     */
    @Override
    public List<String> getColumnTypes(String tableId, boolean withHidden, boolean withBusiness) {
        List<String> types = tableDefinitionDao.getTypes(tableId, withHidden, withBusiness);
        return types;
    }

    @Override
    public List<String> getCommons(String tableId, boolean withHidden, boolean withBusiness) {
        List<String> commons = tableDefinitionDao.getCommons(tableId, withHidden, withBusiness);
        return commons;
    }

    @Override
    public String isExists(String tableId) {
        return tableDefinitionDao.isExists(tableId);
    }

    @Override
    public List<ColumnDesign> getColumnDesigns(String tableId, boolean withHidden, boolean withBusiness) {
        return tableDefinitionDao.getColumnDesigns(tableId, withHidden, withBusiness);
    }

    /**
     * 获取校验文件位置
     * @param tableId 表标识
     * @return
     */
    @Override
    public String getCheckFilePath(String tableId) {
        return tableDefinitionDao.getCheckFilePath(tableId);
    }

    @Override
    public String getTableName(String loginId, String tableId) {
        if (StringUtils.isBlank(loginId)) {
            throw new VerifyFailException("参数无效");
        }
        String tableName = this.isExists(tableId);
        if (StringUtils.isBlank(tableName)) {
            throw new VerifyFailException("您要操作的表不存在或非系统建表");
        }
        return tableName;
    }

    @Override
    public String getTableComment(String loginId, String tableId) {
        if (StringUtils.isBlank(loginId)) {
            throw new VerifyFailException("参数无效");
        }
        String tableName = tableDefinitionDao.getTableComment(tableId);
        if (StringUtils.isBlank(tableName)) {
            throw new VerifyFailException("您要操作的表不存在或非系统建表");
        }
        return tableName;
    }


}
